import configparser, dbus

class Config():
    
    logFormat = None
    logDateTimeFormat = None
    logLevel = None
    logFile = None
    host = None
    port = None
    bufferSize = None
    authMethod = None
    qrImg = None
    cfgData = None
    
    blacklist = None
    
    @classmethod
    def load(cls):
        pwd = None
        #cls.config = configparser.ConfigParser()
        try:
            bus = dbus.SessionBus()
            app = 'org.LinRemote.Server'
            path = '/org/LinRemote/Server'
            interface = 'org.LinRemote.Server'
            obj = bus.get_object(app, path)
            DBusInterface = dbus.Interface(obj, interface)
            pwd = getattr(DBusInterface, 'pwd')()
        except Exception:
            pass
        config = configparser.RawConfigParser()
        config.read(str(pwd) + '/LinRemoteServer.cfg')
        cls.cfgData = config

        try:
            cls.logFormat = config['log']['format']
        except (KeyError, ValueError):
            cls.logFormat = '[%(asctime)s.%(msecs)03d] %(levelname)s: %(message)s'
        
        try:
            cls.logDateTimeFormat = config['log']['dateTimeFormat']
        except (KeyError, ValueError):
            cls.logDateTimeFormat = '%Y-%m-%d %I:%M:%S'
        
        try:
            cls.logLevel = config['log']['level']
        except (KeyError, ValueError):
            cls.logLevel = 'INFO'
        
        try:
            cls.logFile = config['log']['file']
        except (KeyError, ValueError):
            cls.logFile = 'linRemoteServer.log'
            
        try:
            cls.host = config['socket']['host']
        except (KeyError, ValueError):
            cls.host = ''
        try:
            cls.port = int(config['socket']['port'])
        except (KeyError, ValueError):
            cls.port = 5005
        try:
            cls.bufferSize = int(config['socket']['bufferSize'])
        except (KeyError, ValueError):
            cls.bufferSize = 4096
        try:
            cls.authMethod = config['authorization']['method']
        except (KeyError, ValueError):
            cls.authMethod = 'qrcode'
        if cls.authMethod == 'qrcode':
            try:
                cls.qrImg = config['authorization']['qrImg']
            except (KeyError, ValueError):
                cls.qrImg = '/tmp/lr.png'
        try:
            cls.blacklist = str(config['blacklist']['IPlist'])
        except (KeyError, ValueError):
            cls.blacklist = ''
            return False
            
        return True